package com.baijiayun.liveuibase.devicetesting.fragment

import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.content.pm.PackageManager
import android.graphics.*
import android.hardware.camera2.*
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.HandlerThread
import android.util.Size
import android.view.*
import android.widget.FrameLayout
import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.baijiayun.livecore.utils.LPRxUtils
import com.baijiayun.liveuibase.R
import com.baijiayun.liveuibase.devicetesting.DeviceTestingViewModel
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.bjy_base_fragment_device_testing.*
import kotlinx.android.synthetic.main.bjy_base_fragment_device_testing_camera.*
import java.lang.Long.signum
import java.util.*
import java.util.concurrent.Semaphore
import java.util.concurrent.TimeUnit


@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
class DeviceTestingFragment : Fragment() {
    private var disposableOfTime: Disposable? = null
    private lateinit var viewModel: DeviceTestingViewModel

    private val testNetFragment by lazy {
        DeviceTestingNetFragment()
    }

    private val testCameraFragment by lazy {
        DeviceTestingCameraFragment()
    }

    private val testSpeakerFragment by lazy {
        DeviceTestingSpeakerFragment()
    }

    private val testMicFragment by lazy {
        DeviceTestingMicFragment()
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.bjy_base_fragment_device_testing, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel = ViewModelProvider(requireActivity()).get(DeviceTestingViewModel::class.java)
        initView()
        initAction()
    }

    private fun initView() {
        if (viewModel.isRepeatCheck) {
            bjy_base_fragment_device_testing_net_check_result_iv.apply {
                visibility = View.VISIBLE
                isEnabled = viewModel.netResult
            }
            bjy_base_fragment_device_testing_camera_check_result_iv.apply {
                visibility = View.VISIBLE
                isEnabled = viewModel.cameraResult
            }
            bjy_base_fragment_device_testing_speaker_check_result_iv.apply {
                visibility = View.VISIBLE
                isEnabled = viewModel.speakerResult
            }
            bjy_base_fragment_device_testing_mic_check_result_iv.apply {
                visibility = View.VISIBLE
                isEnabled = viewModel.micResult
            }
            bjy_base_fragment_device_testing_net_check_iv.apply {
                setOnClickListener {
                    if (testNetFragment.isVisible) {
                        return@setOnClickListener
                    } else {
                        onTestNet()
                    }
                }
                isEnabled = true
            }
            bjy_base_fragment_device_testing_camera_check_iv.apply {
                setOnClickListener {
                    if (testCameraFragment.isVisible) {
                        return@setOnClickListener
                    } else {
                        onTestCamera()
                    }
                }
                isEnabled = true
            }
            bjy_base_fragment_device_testing_speaker_check_iv.apply {
                setOnClickListener {
                    if (testSpeakerFragment.isVisible) {
                        return@setOnClickListener
                    } else {
                        onTestSpeaker()
                    }
                }
                isEnabled = true
            }
            bjy_base_fragment_device_testing_mic_check_iv.apply {
                setOnClickListener {
                    if (testMicFragment.isVisible) {
                        return@setOnClickListener
                    } else {
                        onTestMic()
                    }
                }
                isEnabled = true
            }
            bjy_base_fragment_device_testing_line_1.setBackgroundColor(ContextCompat.getColor(context!!, R.color.bjy_base_device_testing_main_text_color))
            bjy_base_fragment_device_testing_line_2.setBackgroundColor(ContextCompat.getColor(context!!, R.color.bjy_base_device_testing_main_text_color))
            bjy_base_fragment_device_testing_line_3.setBackgroundColor(ContextCompat.getColor(context!!, R.color.bjy_base_device_testing_main_text_color))
        } else {
            bjy_base_fragment_device_testing_net_check_result_iv.visibility = View.INVISIBLE
            bjy_base_fragment_device_testing_camera_check_result_iv.visibility = View.INVISIBLE
            bjy_base_fragment_device_testing_speaker_check_result_iv.visibility = View.INVISIBLE
            bjy_base_fragment_device_testing_mic_check_result_iv.visibility = View.INVISIBLE
            bjy_base_fragment_device_testing_net_check_iv.isEnabled = false
            bjy_base_fragment_device_testing_camera_check_iv.isEnabled = false
            bjy_base_fragment_device_testing_speaker_check_iv.isEnabled = false
            bjy_base_fragment_device_testing_mic_check_iv.isEnabled = false
        }
        onTestNet()
    }

    private fun initAction() {
        viewModel.getTestStepChange()
                .observe(this, {
                    when (it) {
                        DeviceTestingViewModel.TestStep.TestCamera -> onTestCamera()
                        DeviceTestingViewModel.TestStep.TestSpeaker -> onTestSpeaker()
                        DeviceTestingViewModel.TestStep.TestMic -> onTestMic()
                        DeviceTestingViewModel.TestStep.TestEnd -> onTestEnd()
                        else -> return@observe
                    }
                })
    }

    private fun onTestNet() {
        bjy_base_fragment_device_testing_title_tv.text = getText(R.string.bjy_base_device_testing_check_net)
        childFragmentManager.beginTransaction().replace(R.id.bjy_base_fragment_device_testing_container, testNetFragment).commit()
        bjy_base_fragment_device_testing_net_check_iv.isEnabled = true
    }

    private fun onTestCamera() {
        bjy_base_fragment_device_testing_net_check_result_iv.apply {
            visibility = View.VISIBLE
            isEnabled = viewModel.netResult
        }
        bjy_base_fragment_device_testing_title_tv.text = getText(R.string.bjy_base_device_testing_check_camera)
        childFragmentManager.beginTransaction().replace(R.id.bjy_base_fragment_device_testing_container, testCameraFragment).commit()
        bjy_base_fragment_device_testing_camera_check_iv.isEnabled = true
        bjy_base_fragment_device_testing_line_1.setBackgroundColor(ContextCompat.getColor(context!!, R.color.bjy_base_device_testing_main_text_color))

    }

    private fun onTestSpeaker() {
        bjy_base_fragment_device_testing_camera_check_result_iv.apply {
            visibility = View.VISIBLE
            isEnabled = viewModel.cameraResult
        }
        bjy_base_fragment_device_testing_title_tv.text = getText(R.string.bjy_base_device_testing_check_speaker)
        childFragmentManager.beginTransaction().replace(R.id.bjy_base_fragment_device_testing_container, testSpeakerFragment).commit()
        bjy_base_fragment_device_testing_speaker_check_iv.isEnabled = true
        bjy_base_fragment_device_testing_line_2.setBackgroundColor(ContextCompat.getColor(context!!, R.color.bjy_base_device_testing_main_text_color))

    }

    private fun onTestMic() {
        bjy_base_fragment_device_testing_speaker_check_result_iv.apply {
            visibility = View.VISIBLE
            isEnabled = viewModel.speakerResult
        }
        bjy_base_fragment_device_testing_title_tv.text = getText(R.string.bjy_base_device_testing_check_mic)
        childFragmentManager.beginTransaction().replace(R.id.bjy_base_fragment_device_testing_container, testMicFragment).commit()
        bjy_base_fragment_device_testing_mic_check_iv.isEnabled = true
        bjy_base_fragment_device_testing_line_3.setBackgroundColor(ContextCompat.getColor(context!!, R.color.bjy_base_device_testing_main_text_color))
    }

    private fun onTestEnd() {
        bjy_base_fragment_device_testing_mic_check_result_iv.apply {
            visibility = View.VISIBLE
            isEnabled = viewModel.micResult
        }
    }

    override fun onPause() {
        super.onPause()
        LPRxUtils.dispose(disposableOfTime)
    }
}